package cn.dansj.common.utils.enums;

import java.io.Serializable;

public enum JdbcDriver implements Serializable {
    MySql("mysql", "com.mysql.cj.jdbc.Driver"),
    Tddl("mysql", "com.mysql.cj.jdbc.Driver"),
    DRDS("drds", "com.mysql.cj.jdbc.Driver"),
    Oracle("oracle", "oracle.jdbc.OracleDriver"),
    SQLServer("sqlserver", "com.microsoft.sqlserver.jdbc.SQLServerDriver"),
    PostGreSQL("postgresql", "org.postgresql.Driver"),
    RDBMS("rdbms", "com.alibaba.datax.plugin.rdbms.util.DataBaseType"),
    DB2("db2", "com.ibm.db2.jcc.DB2Driver"),
    ADS("ads", "com.mysql.cj.jdbc.Driver"),
    ClickHouse("clickhouse", "ru.yandex.clickhouse.ClickHouseDriver"),
    KingBaseES("kingbasees", "com.kingbase8.Driver"),
    Oscar("oscar", "com.oscar.Driver");

    private static final long serialVersionUID = GlobalEnv.SerialVersionUID;

    private final String dbName;
    private final String driverName;

    JdbcDriver(String dbName, String driverName) {
        this.dbName = dbName;
        this.driverName = driverName;
    }

    public String getDriverName() {
        return driverName;
    }

    public String getDbName() {
        return dbName;
    }

    public static String getDriverName(String jdbcUrl) {
        try {
            String dbName= DbType.parseUrl(jdbcUrl).name();
            for (JdbcDriver jdbcDriver : JdbcDriver.values()) {
                if (dbName.equalsIgnoreCase(jdbcDriver.getDbName())) {
                    return jdbcDriver.getDriverName();
                }
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }
}
